package com.emergency.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.emergency.config.BusinessException;
import com.emergency.data.entity.EmergencyPointSupplies;
import com.emergency.data.entity.EmergencySuppliesPoint;
import com.emergency.data.vo.EmergencyPointSuppliesVO;
import com.emergency.data.vo.EmergencySuppliesPointDetailsVO;
import com.emergency.data.vo.EmergencySuppliesPointVO;
import com.emergency.http.DefaultResponseCode;
import com.emergency.mapper.EmergencyPointSuppliesMapper;
import com.emergency.mapper.EmergencySuppliesPointMapper;
import com.emergency.service.EmergencySuppliesPointService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * (EmergencySuppliesPoint)应急物资点服务实现类
 *
 * @author makejava
 * @since 2020-09-17 16:18:12
 */
@Service
public class EmergencySuppliesPointServiceImpl extends ServiceImpl<EmergencySuppliesPointMapper, EmergencySuppliesPoint> implements EmergencySuppliesPointService {

    @Resource
    private EmergencySuppliesPointMapper emergencySuppliesPointMapper;

    @Resource
    private EmergencyPointSuppliesMapper emergencyPointSuppliesMapper;

    @Override
    public List<EmergencySuppliesPointVO> getEmergencySuppliesPointList(Long streetId, Long villageId, String name, Integer type) {
        //查询条件
        QueryWrapper<EmergencySuppliesPoint> query = new QueryWrapper<>();
        if (streetId != null) {
            query.eq("street_id", streetId);
        }
        if (villageId != null) {
            query.eq("village_id", villageId);
        }
        if (StrUtil.isNotEmpty(name)) {
            query.like("storage_location", name);
        }
        return emergencySuppliesPointMapper.selectList(query).stream().map(c -> {
            EmergencySuppliesPointVO vo = new EmergencySuppliesPointVO();
            //判断type
            if (type != null) {
                //村社区类型返回null
                if (type == 1 && c.getVillageId() != null) {
                    return null;
                }
                //乡镇类型返回null
                if (type == 2 && c.getVillageId() == null) {
                    return null;
                }
            }
            BeanUtil.copyProperties(c, vo);
            return vo;
        }).filter(Objects::nonNull).collect(Collectors.toList());
    }

    @Override
    public EmergencySuppliesPointDetailsVO getEmergencySuppliesPointDetail(Long id) {
        EmergencySuppliesPoint point = emergencySuppliesPointMapper.selectById(id);
        if (point == null) {
            throw new BusinessException(DefaultResponseCode.EMERGENCY_SUPPLIES_POINT_NOT_EXIST);
        }
        //返回对象VO
        EmergencySuppliesPointDetailsVO detailsVO = new EmergencySuppliesPointDetailsVO();
        BeanUtil.copyProperties(point, detailsVO);
        //应急物资点物资列表
        List<EmergencyPointSuppliesVO> suppliesList = emergencyPointSuppliesMapper.selectList(new QueryWrapper<EmergencyPointSupplies>()
                .eq("emergency_supplies_point_id", id)).stream().map(c -> {
            EmergencyPointSuppliesVO vo = new EmergencyPointSuppliesVO();
            BeanUtil.copyProperties(c, vo);
            return vo;
        }).collect(Collectors.toList());
        detailsVO.setSuppliesList(suppliesList);
        return detailsVO;
    }
}
